package com.centaline.androidsalesblog.overlay;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.busline.BusLineResult;
import com.centaline.androidsalesblog.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于显示一条地铁详情结果的Overlay,自己定义的
 * Created by Vctor on 16/2/4.
 */
public class RailwayLineOverlay extends OverlayManager {

    private final Context context;
    private BusLineResult mBusLineResult = null;

    protected View view;
    protected AppCompatTextView atv_content;

    /**
     * 构造函数
     *
     * @param baiduMap 该BusLineOverlay所引用的 BaiduMap 对象
     */
    public RailwayLineOverlay(Context context, BaiduMap baiduMap) {
        super(baiduMap);
        this.context = context;

        view = LayoutInflater.from(context).inflate(R.layout.layout_map_level, null);
        atv_content = (AppCompatTextView) view.findViewById(R.id.atv_content);
    }

    /**
     * 设置公交线数据
     *
     * @param result 公交线路结果数据
     */
    public void setData(BusLineResult result) {
        this.mBusLineResult = result;
    }

    /**
     * 定位到具体站点
     */
    public void setStation(String stationName) {
        if (mBusLineResult == null || mBusLineResult.getStations() == null) {
            return;
        }
        for (BusLineResult.BusStation station : mBusLineResult.getStations()) {
            if (stationName.contains(station.getTitle()) ||
                    station.getTitle().contains(stationName)) {
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(
                        station.getLocation(), 17f));
                break;
            }
        }
    }

    @Override
    public final List<OverlayOptions> getOverlayOptions() {
        if (mBusLineResult == null || mBusLineResult.getStations() == null) {
            return null;
        }
        List<OverlayOptions> overlayOptionses = new ArrayList<>();
        for (BusLineResult.BusStation station : mBusLineResult.getStations()) {
            atv_content.setText(station.getTitle());
            overlayOptionses.add(new MarkerOptions()
                    .position(station.getLocation())
                    .zIndex(10)
                    .anchor(0.5f, 0.5f)
                    .icon(BitmapDescriptorFactory
                            .fromView(view)));
        }

        List<LatLng> points = new ArrayList<>();
        for (BusLineResult.BusStep step : mBusLineResult.getSteps()) {
            if (step.getWayPoints() != null) {
                points.addAll(step.getWayPoints());
            }
        }
        if (points.size() > 0) {
            overlayOptionses
                    .add(new PolylineOptions().width(14)
                            .color(Color.argb(200, 241, 62, 6)).zIndex(0)
                            .points(points));
        }

        return overlayOptionses;
    }

    /**
     * 覆写此方法以改变默认点击行为
     *
     * @param index 被点击的站点在
     *              {@link BusLineResult#getStations()}
     *              中的索引
     * @return 是否处理了该点击事件
     */
    public boolean onBusStationClick(int index) {
        if (mBusLineResult.getStations() != null
                && mBusLineResult.getStations().get(index) != null) {
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(
                    mBusLineResult.getStations().get(index).getLocation(), 17f));
        }
        return false;
    }

    public final boolean onMarkerClick(Marker marker) {
        if (mOverlayList != null && mOverlayList.contains(marker)) {
            return onBusStationClick(mOverlayList.indexOf(marker));
        } else {
            return false;
        }

    }

    @Override
    public boolean onPolylineClick(Polyline polyline) {
        return false;
    }
}
